﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BaseDatos;

namespace SCHD
{
    public partial class WFFormularios : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        public WFFormularios()
        {
            InitializeComponent();
        }

        private void TsbSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TsbGuardar_Click(object sender, EventArgs e)
        {
            ControlError.SetError(TxtNombre, "");
            ControlError.SetError(TxtDescripcion,"");
            if (string.IsNullOrEmpty(TxtNombre.Text))
            {
                ControlError.SetError(TxtNombre, "Ingrese un Nombre");
            }
            if (string.IsNullOrEmpty(TxtDescripcion.Text))
            {
                ControlError.SetError(TxtDescripcion, "Ingrese una Descripción");
            }

            if (TxtNombre.Text.Length > 0 && TxtDescripcion.Text.Length > 0)
            {
                string usr = Environment.UserName.ToUpper();
                string wks = Environment.MachineName.ToUpper();
                string SQL = "" +
                        "insert into chdformularios (codform, nomform, dscform, usrcre, fchcre, wkscre, usrmod, fchmod, wksmod, flgeli,comando) " +
                        "                     values ((select 'WF'||lpad(substr(max(codform),3)+1,8,'0') from chdformularios),'" + TxtNombre.Text + "','" + TxtDescripcion.Text + "','" + usr + "',sysdate,'" + wks + "','" + usr + "',sysdate,'" + wks + "','0','" + TxtComando.Text + "')";
                ConexionBaseDatos.Conexion.EjecutarComando(SQL, CommandType.Text);
                TxtNombre.Text = "";
                TxtDescripcion.Text = "";
                TxtComando.Text = "";
                CargarGrid();
            }
        }
        protected void CargarGrid()
        {
           DataSet ds = ConexionBaseDatos.Conexion.CrearDataSet("select codform,nomform,dscform,comando from chdformularios where flgeli ='0'", CommandType.Text);
           if (ds.Tables[0].Rows.Count > 0)
           {
               GrdFormularios.DataSource = ds.Tables[0];
           }
        }

        private void WFFormularios_Load(object sender, EventArgs e)
        {
            CargarGrid();
        }

        private void TsbModificar_Click(object sender, EventArgs e)
        {
            string cod = GrdFormularios.CurrentRow.Cells[0].Value.ToString();
            string nombre = GrdFormularios.CurrentRow.Cells[1].Value.ToString();
            string descripcion = GrdFormularios.CurrentRow.Cells[2].Value.ToString();
            string comando = GrdFormularios.CurrentRow.Cells[3].Value.ToString();
            WFFormulario_Edit form = new WFFormulario_Edit(cod, nombre, descripcion, comando);
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                CargarGrid();
            }
        }

        private void TsbEliminar_Click(object sender, EventArgs e)
        {
            try
            {
                string SQL = "update chdformularios set flgeli = '1' where codform = '" + GrdFormularios.CurrentRow.Cells[0].Value.ToString() + "'";
                ConexionBaseDatos.Conexion.EjecutarComando(SQL, CommandType.Text);
                CargarGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
